<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="form" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="兑换活动名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="兑换活动名称"></el-input>
    </el-form-item>
    <el-form-item label="兑换类型" prop="exchangeType">
      <el-input v-model="dataForm.exchangeType" placeholder="兑换类型" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="兑换天数" prop="exchangeDays">
      <el-input v-model="dataForm.exchangeDays" placeholder="兑换天数"></el-input>
    </el-form-item>
    <el-form-item label="兑换码总数" prop="codeCount">
      <el-input v-model="dataForm.codeCount" placeholder="兑换码总数"></el-input>
    </el-form-item>
    <el-form-item label="兑换券生效时间" prop="startTime">
      <el-date-picker
        v-model="dataForm.startTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择兑换券生效时间"
        :picker-options="pickerOptions">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="兑换券过期时间" prop="endTime">
      <el-date-picker
        v-model="dataForm.endTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择兑换券过期时间"
        :picker-options="pickerOptions">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <!-- <el-input v-model="dataForm.status" placeholder="状态(1上架,0下架)"></el-input> -->
      <el-radio-group v-model="dataForm.status" border>
        <el-radio :label="0">下架</el-radio>
        <el-radio :label="1">上架</el-radio>
      </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          name: '',
          exchangeType: 'vip',
          exchangeId: '',
          exchangeDays: '',
          codeCount: '',
          startTime: '',
          endTime: '',
          status: ''
        },
        pickerOptions: { // 设置今天之前日期不可选
          disabledDate (time) {
            let minDate = new Date()
            minDate.setDate(minDate.getDate() - 1)
            return time.getTime() < minDate.getTime()
          }
        },
        dataRule: {
          name: [
            { required: true, message: '兑换活动名称不能为空', trigger: 'blur' }
          ],
          // exchangeType: [
          //   { required: true, message: '兑换类型(vip/book/bookitem等)不能为空', trigger: 'blur' }
          // ],
          exchangeDays: [
            { required: true, message: '兑换天数不能为空', trigger: 'blur' }
          ],
          codeCount: [
            { required: true, message: '兑换码总数不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '兑换券生效时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '兑换券过期时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(1上架,0下架)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = parseInt(id) || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['form'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbexchagegroup/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data.pbExchageGroup
                // this.dataForm.name = data.pbexchagegroup.name
                // this.dataForm.exchangeType = data.pbexchagegroup.exchangeType
                // this.dataForm.exchangeId = data.pbexchagegroup.exchangeId
                // this.dataForm.exchangeDays = data.pbexchagegroup.exchangeDays
                // this.dataForm.codeCount = data.pbexchagegroup.codeCount
                // this.dataForm.startTime = data.pbexchagegroup.startTime
                // this.dataForm.endTime = data.pbexchagegroup.endTime
                // this.dataForm.status = data.pbexchagegroup.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbexchagegroup/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                pbExchageGroup: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
